<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class T_offer extends MY_Model{
	public function __construct()
	{
		parent::__construct();
		$this->table_name = T_OFFER;
	}

	/**
	 * get user by id
	 * @param  string  $select : fields of table need get
	 * @param  integer $id     id of user
	 * @return array|null
	 */
	public function get_data_by_id($id = -1, $select = '*'){
		if($id == -1 || !is_numeric($id)){
			return null;
		}

		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}

		$this->db->where(OFFER_ID ." = $id LIMIT 1");

		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * get data by property
	 * @param  string $select : fields of table need get
	 * @param  array  $where  conditions
	 * @return array|null
	 */
	public function get_data_by_property($where = array(), $select = '*'){
		if($where == null || !is_array($where)){
			return null;
		}

		// query
		if(strcmp($select, '*') == 0){
			$this->db->select();
		}else{
			$this->db->select($select);
		}
		$this->db->where($where);
		$result = $this->db->get($this->table_name);
		$result = $result->result_array();
		return $result;
	}

	/**
	 * insert data into talbe
	 * @param array   $data   array data insert
	 * @param integer $result if 0 return id inserted else 1 return data user inserted
	 * @return null|integer|array
	 */
	public function set_data($data = array(), $result = 0){
		if($data == null || !is_array($data)){
			return null;
		}

		// insert rows
		$this->db->insert($this->table_name, $data);
		// get id
		$last_id = $this->db->insert_id();
		//return
		if($result == 0){
			return $last_id;
		}else{
			return $this->get_data_by_id($last_id);
		}
	}

	/**
	 * update data by id
	 * @param  array   $data : data update
	 * @param  integer $id
	 * @return integer
	 */
	public function update_data_by_id($data = array(), $id = -1){
		if($data == null || !is_array($data) || $id == -1 || !is_numeric($id)){
			return null;
		}

		//condition
		$this->db->where(OFFER_ID ." = $id LIMIT 1");
		$this->db->update($this->table_name, $data);
		return $this->db->affected_rows();
	}

	/**
	 * update data
	 * @param  array  $data
	 * @param  array  $where : conditions update
	 * @return integer
	 */
	public function update_data_by_property($data = array(), $where = array()){
		if($data == null || !is_array($data) || $where == null || !is_array($where)){
			return null;
		}

		$this->db->where($where);
		$this->db->update($this->table_name, $data);
		return $this->db->affected_rows();
	}

	public function unset_data_by_id($offer_id = -1){
		if($offer_id == -1 || !is_numeric($offer_id)){
			return 'Value invalid';
		}

		$this->db->delete($this->table_name, array(OFFER_ID => $offer_id));
		return 1;
	}
	
	public function get_offering($productId = -1){
		if($productId == -1 || !is_numeric($productId)){
			return 'Params productId invalid';
		}

		$this->db->select('o.*, u.'.USER_NAME.',u.'.USER_AVATAR_PATH);
		$this->db->from($this->table_name . ' as o');
		$this->db->where(OFFER_PRODUCT_ID, $productId);
		$this->db->join('userinfo as u', 'u' . '.' . USER_ID .' = '. 'o'. '.'. OFFER_USER_ID);
		$arrOffer = $this->db->get();

		$arrOffer = $arrOffer->result_array();
		return $arrOffer;
	}

	public function get_history_offer($user_id){
		$sql = "SELECT T.*,offer.st_OfferPrice,offer.st_CreatedDate AS NgayOfferGanNhat FROM (SELECT T.*,product.st_ProductName,product.st_CreatedDate,product.st_Price AS GiaDang,product.st_AvatarPath,(product.st_OfferId > 0) AS Daban,product.st_OfferId FROM (SELECT product.st_ProductId, COUNT(*) AS SoLuongOffer FROM product LEFT JOIN offer ON product.st_ProductId=offer.st_ProductId WHERE product.st_UserId=$user_id GROUP BY product.st_ProductId) AS T JOIN product ON product.st_ProductId=T.st_ProductId) AS T LEFT JOIN offer ON T.st_OfferId=offer.st_OfferId";

        $result = $this->db->query($sql);
        $result = $result->result_array();
        return $result;
	}
}